/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2017年7月3日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.thread.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Type AppleBasket
 * @Desc 苹果篮子例子，生产者消费者
 * @author boboge
 * @date 2017年7月3日
 * @Version V1.0
 */
public class AppleBasket {

    public static class Basket {
        BlockingQueue<String> basket = new ArrayBlockingQueue<>(3);

        public void pickApple() throws InterruptedException {
            basket.put("pick apple");
        }

        public void selllApple() throws InterruptedException {
            basket.take();
        }

        public static void testBasket() {
            final Basket basket = new Basket();
            class AppleTree implements Runnable {
                /*
                 * (non-Javadoc)
                 * 
                 * @see java.lang.Runnable#run()
                 */
                @Override
                public void run() {
                    try {
                        while (true) {
                            System.out.println("摘一个苹果" + System.currentTimeMillis());
                            basket.pickApple();
                            System.out.println("摘完一个苹果" + System.currentTimeMillis());
                            Thread.sleep(300);
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
            class Seller implements Runnable {
                /*
                 * (non-Javadoc)
                 * 
                 * @see java.lang.Runnable#run()
                 */
                @Override
                public void run() {
                    try {
                        while (true) {
                            System.out.println("卖一个苹果" + System.currentTimeMillis());
                            basket.selllApple();
                            ;
                            System.out.println("付完钱交易结束" + System.currentTimeMillis());
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

            ExecutorService service = Executors.newCachedThreadPool();
            AppleTree appleTree = new AppleTree();
            Seller seller = new Seller();
            service.submit(appleTree);
            service.submit(seller);
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                // TODO: handle exception
            }
            service.shutdownNow();
        }
    }

    public static void main(String[] args) {
        // Basket.testBasket();
        int i = 0;
        i = i++;
        System.out.println(i);
    }

}
